package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

/*
   Author: mahaiyuan
   Date: 2021/6/8 10:42 下午
   Description:
*/
func main() {
	//postJson()
	postForm()
}

func postJson() {
	url := "http://localhost:8888/student/create"
	contentType := "application/json"
	data := "{\"studentNo\":\"20210010001\", \"name\": \"张三\"}"
	resp, err := http.Post(url, contentType, strings.NewReader(data))
	if err != nil {
		fmt.Println("post error.", err)
		return
	}
	defer resp.Body.Close()
	// 读取响应结果
	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("read resp data error", err)
		return
	}
	fmt.Println(string(bytes))
}
func postForm() {
	url := "http://localhost:8888/student/create"
	contentType := "application/x-www-form-urlencoded"
	data := "studentNo=20210010001&name=张三"
	resp, err := http.Post(url, contentType, strings.NewReader(data))
	if err != nil {
		fmt.Println("post error.", err)
		return
	}
	defer resp.Body.Close()
	// 读取响应结果
	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("read resp data error", err)
		return
	}
	fmt.Println(string(bytes))
}
